package com.dfhc.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class JsonToMap {

	public static Map<String, Object> parseJSON2Map(String bizData) {
		Map<String, Object> ret = new HashMap<String, Object>();
		
    	try{
    		JSONObject bizDataJson = JSONObject.parseObject(bizData);
    		for(Object key:bizDataJson.keySet()){
    			Object value = bizDataJson.get(key);
    			if(value instanceof JSONArray){
    				List<Object> list = new ArrayList<Object>();  
                    Iterator<Object> it = ((JSONArray) value).iterator();

                    while(it.hasNext()){  
                    	
                    	 Object obj = (Object)it.next(); 
                    	 
                    	if(obj instanceof String){
                    		list.add(obj.toString());
                    	}else if(obj instanceof JSONObject ){
                    		
                    		list.add(parseJSON2Map(obj.toString()));  
                    	}
                    	
//                        JSONObject json2 = (JSONObject)it.next();  
                    }  
                    ret.put(String.valueOf(key),list);  
    			}else{
    				ret.put(String.valueOf(key), String.valueOf(value));
    			}
    		}
    	}catch(Exception e){
    		e.printStackTrace();
    	}
		return ret;
	}
	
	
	
	
	/**
	 * json字符串转为json只转换一层
	 * @param bizData
	 * @return
	 */
	public static  Map<String, Object> parseSimpleJSON2Map(String bizData) {
		
		Map<String, Object> ret = new HashMap<String, Object>();
		
    	try{
    		JSONObject bizDataJson = JSONObject.parseObject(bizData);
    		for(Object key:bizDataJson.keySet()){
    			Object value = bizDataJson.get(key);
    			ret.put(String.valueOf(key), String.valueOf(value));
    			
    		}
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    
		return ret;
	}
	
	
	
	
}
